﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace DigitalSum.Web.UI.WebControls
{
    public class CompositeView : CompositeDataBoundControl
    {
        #region PROPERTIES
        public System.Web.UI.WebControls.FormViewMode _currentMode;
        public System.Web.UI.WebControls.FormViewMode CurrentMode
        {
            get { return _currentMode; }
        }

        private ITemplate _ItemTemplate;
        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(SimpleContainer))]
        public virtual ITemplate ItemTemplate
        {
            get { return _ItemTemplate; }// get
            set { _ItemTemplate = value; }// set
        }// property        

        private object _dataItem;
        [BrowsableAttribute(false)]
        public virtual Object DataItem
        {
            get { return _dataItem; }
        }

        #endregion

        #region CONSTRUCTORS

        protected CompositeView() : base() { _Init(); }

        #region INITIALIZATIONS

        ContextManager _manager;
        private void _Init()
        {
            _manager = new ContextManager();
        }

        #endregion

        #endregion

        #region METHODS

        public virtual void ChangeMode(FormViewMode newMode)
        {
            _currentMode = newMode;
        }

        protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            if (!_manager.Launched)
                return 0;

            int count = 0;
            if (dataBinding && dataSource != null)
            {
                foreach (object dataItem in dataSource)
                {
                    if (ItemTemplate != null)
                    {
                        // caching data for container
                        _dataItem = dataItem;

                        // create instance of SimpleItem control
                        SimpleContainer item = new SimpleContainer(dataItem, count++);

                        // instantiate in new item object
                        ItemTemplate.InstantiateIn(item);

                        // add item to Controls collection
                        this.Controls.Add(item);

                        if (dataBinding)
                        {
                            // need to support <%# %> expressions
                            item.DataBind();
                        }
                    }
                    // add a break between each item
                    this.Controls.Add(new LiteralControl("<br />"));
                }
            }
            return count;
        }

        protected override void OnLoad(EventArgs e)
        {
            if (_manager.Launched)
            {
                if (this.Visible)
                    base.OnLoad(e);
                else
                    RequiresDataBinding = false;
            }
            else
                base.OnLoad(e);
        }

        public override void DataBind()
        {
            if (_manager.Launched)
            {
                if (this.Visible)
                    base.DataBind();
                else
                {
                }
            }
            else
                base.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        #endregion
    }

    public class SimpleContainer : Control, IDataItemContainer
    {
        #region CONSTRUCTORS

        public SimpleContainer(object dataItem, int index)
            : base()
        {
            _Init();
            ID = index.ToString();
            _dataItem = dataItem;
            _dataItemIndex = _displayIndex = index;
        }

        #region INITIALIZATIONS

        ContextManager _manager;
        private void _Init()
        {
            _manager = new ContextManager();
            if (!_manager.Launched) { throw new InvalidOperationException(); }
        }

        #endregion

        #endregion

        private object _dataItem;
        public object DataItem
        {
            get { return _dataItem; }
        }

        private int _dataItemIndex;
        public int DataItemIndex
        {
            get { return 0; }
        }

        private int _displayIndex;
        public int DisplayIndex
        {
            get { return 0; }
        }
    }

}
